package blog.com.mw.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import blog.com.mw.base.ResultData;
import blog.com.mw.dao.UserDao;
import blog.com.mw.pojo.po.UserPO;
import blog.com.mw.pojo.vo.UserVO;
import blog.com.mw.service.UserService;
import blog.com.mw.util.EmailUtil;

@Service("userService")
public class UserServiceImpl implements UserService {
	
	@Autowired
	private static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
	
	/**
	 * 注入UserDao
	 */
	@Autowired
	private UserDao dao;

	public UserDao getUserDao() {
		return this.dao;
	}
	
	public void setUserDao(UserDao userDao) {
		this.dao = userDao;
	}
	
	

	@Override
	public ResultData getAllUser(int pageNum,int pageSize,String keyword) {
		logger.info("进入getAllUser方法");
		ResultData rs = null;
		Map<String,Object> data = null;
		try {
			PageHelper.startPage(pageNum, pageSize);
			List<UserPO> userPos = dao.getAllUser(keyword);
			if(userPos != null && !userPos.isEmpty()) 
			{	
				List<UserVO> userVos = new ArrayList<>();
				userPos.forEach(item->{
					item.setPassword(null);
					UserVO vo =new UserVO();
					vo.poToVo(item);
					userVos.add(vo);
				});
				PageInfo<UserVO> pageInfo = new PageInfo<UserVO>(userVos);
				pageInfo.setTotal(dao.getAllUser(keyword).size());
				data = new HashMap<>();
				data.put("data", pageInfo);
				rs = ResultData.getOk();
				rs.setMsg("获取成功");
				rs.setData(data);
			}else {
				rs = ResultData.getFail();
				rs.setMsg("获取失败！");
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		logger.info("退出getAllUser方法");
		return  rs;
	}
	
	@Override
	public ResultData getUserById(int id) {
		logger.info("进入getUserById方法");
		ResultData rs = null;
		Map<String,Object> data = null;
		try {
			UserPO po = dao.getUserById(Long.valueOf(id));
			if(po != null) {
				data = new HashMap<>();
				data.put("data", po);
				rs = ResultData.getOk();
				rs.setMsg("获取成功");
				rs.setData(data);
			}else {
				rs = ResultData.getFail();
				rs.setMsg("不存在该用户！");
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		logger.info("退出getUserById方法");
		return rs;
	}

	@Override
	public ResultData delUser(List<Integer> ids) {
		logger.info("进入delUser方法");
		ResultData rs = null;
		try {
			if(ids.size()==0) {
				rs = ResultData.getFail();
				rs.setMsg("参数不得为空！");
				return rs;
			}
			int delSize = dao.delUser(ids);
			if(delSize>0) {
				rs = ResultData.getOk();
				rs.setMsg(delSize==ids.size()?"删除成功！":"部分数据未删除成功！");
			}else {
				rs = ResultData.getFail();
				rs.setMsg("不存在该用户！");
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		logger.info("退出delUser方法");
		return rs;
	}

	@Override
	public ResultData updateUser(UserVO userVo) {
		logger.info("进入updateUser方法");
		ResultData rs = null;
		Map<String,Object> data = null;
		try {
			if(ObjectUtils.isEmpty(userVo)) {
				rs = ResultData.getFail();
				rs.setMsg("参数不得为空！");
				return rs;
			}
			UserPO user = new UserPO();
			userVo.voToPo(user);
			if(dao.updateUser(user) > 0) 
			{	user.setAcount(null);
				data = new HashMap<>();
				user.setPassword(null);
				data.put("data", user);
				rs = ResultData.getOk();
				rs.setMsg("修改成功");
				rs.setData(data);
			}else {
				rs = ResultData.getFail();
				rs.setMsg("修改失败！");
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		logger.info("退出updateUser方法");
		return  rs;
	}
	@Transactional
	@Override
	public ResultData login(UserVO userVo) {
		logger.info("进入login方法");
		ResultData rs = null;
		Map<String,Object> data = null;
		try {
			if(ObjectUtils.isEmpty(userVo)) {
				rs = ResultData.getFail();
				rs.setMsg("参数不得为空！");
				return rs;
			}
			UserPO user = new UserPO();
			userVo.voToPo(user);
			data = new HashMap<>();
			UserPO result = dao.getUserByAcount(user.getAcount());
			//判断用户是否存在
			if(result == null) {
				data.put("data", result);
				rs = ResultData.getFail();
				rs.setMsg("用户不存在！");
				rs.setData(data);
			}else {
				user.setId(result.getId());
				//用户认证
				UserPO res = dao.login(user);
				if(res==null) {
					rs =ResultData.getFail();
					rs.setMsg("密码错误！");
				}else {
					res.setPassword(null);
					rs = ResultData.getOk();
					rs.setMsg("登录成功");					
				}
				data.put("data", res);
				rs.setData(data);
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		
		logger.info("退出login方法");
		return rs;
	}

	@Override
	public ResultData register(UserVO userVo) {
		logger.info("进入register方法");
		ResultData rs = null;
		Map<String,Object> data = null;
		try {
			if(ObjectUtils.isEmpty(userVo)) {
				rs = ResultData.getFail();
				rs.setMsg("参数不得为空！");
				return rs;
			}
			UserPO user = new UserPO();
			userVo.voToPo(user);
			String account = null;
			UserPO testAccount = null;
			while(account==null) {
				account = String.valueOf(new Random().nextInt(899999) + 100000);
				testAccount = dao.getUserByAcount(account);
				if(testAccount != null) account = null;
			}
			user.setAcount(account);
			if(dao.addUser(user) > 0) 
			{	
				data = new HashMap<>();
				user.setPassword(null);
				user.setCtime(null);
				userVo.poToVo(user);
				data.put("data", userVo);
				rs = ResultData.getOk();
				rs.setMsg("注册成功");
				rs.setData(data);
			}else {
				rs = ResultData.getFail();
				rs.setMsg("注册失败！");
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		logger.info("退出register方法");
		return  rs;
	}

	@Override
	public ResultData resetPwd(int id) {
		logger.info("进入resetPwd方法");
		ResultData rs = null;
		try {
			UserPO user = new UserPO();
			user.setId(Long.valueOf(Long.valueOf(id)));
			user.setPassword("123456");
			if(dao.resetPwd(user) > 0) 
			{	
				rs = ResultData.getOk();
				rs.setMsg("重置成功");
			}else {
				rs = ResultData.getFail();
				rs.setMsg("重置失败！");
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		logger.info("退出resetPwd方法");
		return  rs;
	}

	@Override
	public ResultData getVeriCode(String mail) {
		logger.info("进入getVeriCode方法");
		ResultData rs = null; 
		Map<String,Object> data = new HashMap<>();
		try {
			if(StringUtils.isEmpty(mail)) {
				rs = ResultData.getFail();
				rs.setMsg("参数不得为空！");
				return rs;
			}
		 	int veriCode = new Random().nextInt(8999) + 1000;
			 if(EmailUtil.sendEmailAndFile(mail,veriCode)) {
				 rs = ResultData.getOk();
				 data.put("veriCode", veriCode);
				 rs.setData(data);
			 }else {
				 rs = ResultData.getFail();
			 }
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		logger.info("退出getVeriCode方法");
		return  rs;
	}
}
